package com.server.service;

import com.server.util.FreeMarkerHelp;
import com.server.util.MyFileUtils;
import freemarker.template.Template;

import java.io.File;
import java.io.FileWriter;
import java.io.Writer;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: sunguangzhu
 * Date: 14-2-14
 * Time: 下午5:26
 * To change this template use File | Settings | File Templates.
 */
public class GeneratorService {
    //生成文件公用方法
    private void generateFile(String outputFilePath, String templateName, Map<String, Object> root) throws Exception {
        String templateDirPath = GeneratorParams.templateDirPath;
        File templateDir = new File(templateDirPath + "\\template\\permission");
        freemarker.template.Configuration cfg = FreeMarkerHelp.getCfg();
        //从什么地方加载模板文件
        cfg.setDirectoryForTemplateLoading(templateDir);
        //加载模板
        Template template = cfg.getTemplate(templateName + ".ftl");
        //定义数据
        Writer out = new FileWriter(outputFilePath);
        template.process(root, out);
        out.flush();
        out.close();
    }

    public void generatePom(GeneratorParams generatorParams) {
        String path = generatorParams.getPath();
        String templateName = generatorParams.getTemplateName();
        Map<String, Object> root = generatorParams.getRoot();
        //定义输出
//        File outputDir = MyFileUtils.makeDir(hardDisk, projectName + "/");
        String outputDir = path;
        MyFileUtils.makeDir(outputDir);
        String outputFilePath = outputDir  + "pom" + ".xml";
        File file = new File(outputFilePath);
        try {
            this.generateFile(outputFilePath, templateName, root);
        } catch (Exception e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }
    }

    public void generateXml(GeneratorParams generatorParams) {
        String path = generatorParams.getPath();
        String templateName = generatorParams.getTemplateName();
        Map<String, Object> root = generatorParams.getRoot();
        //定义输出
        String outputDir =path;
        MyFileUtils.makeDir(outputDir);
        String outputFilePath = outputDir +generatorParams.getResourcePath()+ generatorParams.getFilename() + generatorParams.getResources_suffix();
        File file = new File(outputFilePath);
        try {
            this.generateFile(outputFilePath, templateName, root);
        } catch (Exception e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }
    }

    /**
     *
     * @param generatorParams
     * @throws Exception
     */
    public void generate(GeneratorParams generatorParams) throws Exception {
        String path = generatorParams.getPath();
        String packageName = generatorParams.getPackageName();
        String fileName = generatorParams.getFilename();
        String templateName = generatorParams.getTemplateName();
        Map<String, Object> root = generatorParams.getRoot();
        String outputDir = path+"src/main"+"/java/" + packageName.replace(".", "/");
        MyFileUtils.makeDir(outputDir);
        String outputFilePath = outputDir +"/"+ fileName + ".java";
        File file = new File(outputFilePath);
        this.generateFile(outputFilePath, templateName, root);
    }

}
